//
//  GeRenXinXiViewController.m
//  firstpoint
//
//  Created by 泽中 on 2017/6/28.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "GerRenXinXiViewController.h"
#import "BindingPhoneViewController.h"
#import "OwnedCompanyViewController.h"
#import "SetPasswordViewController.h"
#import "ZZYPhotoHelper.h"
@interface GerRenXinXiViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    UITableView*tabView;//表
    NSArray*titleArr;
    NSDictionary*dataDic;
    UIImageView*imgVIew;
    UIImage*upImage;

}

@end

@implementation GerRenXinXiViewController
-(void)myInfoHttpRequest
{  NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"/user/accountInfo.api?access_token=%@",tokenStr];
    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        
        dataDic= [PublicTools dictionaryWithJsonString:responseObject[@"data"][@"userDTO"]];
        NSString*Str1=[NSString stringWithFormat:@"账号:%@",dataDic[@"userName"]];
        NSString*Str2=[NSString stringWithFormat:@"等级%@",dataDic[@"level"]];
        
        
      
        
        NSString*Str5=[NSString stringWithFormat:@"姓名:%@",dataDic[@"realName"]];
        NSString*Str6;
        int xingbieNum=[dataDic[@"gender"]intValue];
        if (xingbieNum==1) {
            Str6=@"性别: 男";
        }else{
            Str6=@"性别: 女";
        }
        NSString*Str7=[NSString stringWithFormat:@"生日:%@",dataDic[@"birthday"]];
        
        
        
        
        titleArr=@[@[Str1,Str2,@"成长值"],@[@"头像",Str5,Str6,Str7],@[@"所属公司",@"登录密码",@"绑定手机号"]];
        NSURL*url=[NSURL URLWithString:LINK_BASE_URL(dataDic[@"headUrl"])];
        [imgVIew setImageWithURL:url];

        [tabView reloadData];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self myInfoHttpRequest];
    self.title=@"个人信息";
    titleArr=@[@[@"账号",@"等级",@"成长值"],@[@"头像",@"姓名",@"性别",@"生日"],@[@"所属公司",@"登录密码",@"绑定手机号"]];
    self.view.backgroundColor=[UIColor whiteColor];
    [self setTab];
}
-(void)setTab
{
    tabView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, width_screen, height_screen-64) style:1];
    tabView.separatorStyle=0;
    tabView.delegate=self;
    tabView.dataSource=self;
    [self.view addSubview:tabView];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 3;
    }
    if (section==1) {
        return 4;
    }
    if (section==2) {
        return 3;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        
        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 49, width_screen, 1)];
        view.backgroundColor=LRRGBColor(239, 239, 244);
        [cell.contentView addSubview:view];
        
        if (indexPath.section==1&&indexPath.row==0) {
            imgVIew=[[UIImageView alloc]initWithFrame:CGRectMake(width_screen-100, 5, 40, 40)];
            NSURL*url=[NSURL URLWithString:LINK_BASE_URL(dataDic[@"headUrl"])];
            [imgVIew setImageWithURL:url];
            [cell.contentView addSubview:imgVIew];
        }
    }
    
    cell.textLabel.text=titleArr[indexPath.section][indexPath.row];
    cell.textLabel.font=[UIFont systemFontOfSize:14];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        if (indexPath.row==0) {
            __weak typeof(self) weakSelf =self;
            [[ZZYPhotoHelper shareHelper] showImageViewSelcteWithResultBlock:^(id data) {
               
               imgVIew.image=data;
                upImage=data;
                [weakSelf uppictureRequset];
            }];

        }
    }
    
    if (indexPath.section==2) {
        if (indexPath.row==0) {
            OwnedCompanyViewController*vc=[OwnedCompanyViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==2) {
            [self.navigationController pushViewController:[BindingPhoneViewController new] animated:YES];
        }
        if (indexPath.row==1) {
            [self.navigationController pushViewController:[SetPasswordViewController new] animated:YES];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSLog(@"sss");
}
-(void)uppictureRequset{
    NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"common/file/upload.api?access_token=%@",tokenStr];
    
    NSData*data;
    upImage=  [self scaleToSize:upImage size:CGSizeMake(150, 150)];
    if (UIImagePNGRepresentation(upImage)) {
        data=  UIImagePNGRepresentation(upImage);
        
    }else
    {
        data=  UIImageJPEGRepresentation(upImage,1);
        
    }
    NSLog(@"%@",data);
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         
                                                         @"text/html",
                                                         
                                                         @"image/jpeg",
                                                         
                                                         @"image/png",
                                                         
                                                         @"application/octet-stream",
                                                         
                                                         @"text/json",
                                                         
                                                         nil];
    
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    
    [manager POST:LINK_BASE_URL(urlStr)  parameters:@{@"userId":userid,@"category":@"1"} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //        UIImage *image = [UIImage imageNamed:@"gauge.png"];
        //
        //        NSData *data = UIImagePNGRepresentation(image);
        
        //上传的参数(上传图片，以文件流的格式)
        
        [formData appendPartWithFileData:data
         
                                    name:@"uploadFile"
         
                                fileName:@"gauge.jpg"
         
                                mimeType:@"image/jpg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功");
        NSLog(@"%@",responseObject);
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"上传失败%@",error);
        
        
        
    }];
    
}

-(UIImage *)scaleToSize:(UIImage *)image size:(CGSize)size
{
    //创建一个bitmap的context
    //并把他设置成当前的context
    UIGraphicsBeginImageContext(size);
    //绘制图片的大小
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    //从当前context中创建一个改变大小后的图片
    UIImage *endImage=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return endImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
